/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

import "privacysettings.js" as PS

Rectangle {
    id: routedetailsdlg
    width: 350
    height: 280 //cancelid.y + cancelid.height + 10
    gradient: Gradient {
        GradientStop {
            position: 0.44
            color: "#abd1ee"
        }

        GradientStop {
            position: 1
            color: "#000000"
        }
    }
    z: 50

    property alias routename: routenameinput.text
    property alias routedescriptions: routedescriptioninput.text
    property alias routetime: routetimeinput.text
    property string routerepeat: "No repeat"
    property string showownername: "0"
    property string showownerhometown: "0"
    property string showownerphone: "0"
    property string routekarpoid: "0"
    property string routecarpoolkarpoid: "0"

    property int carpoolarrind: -1

    property variant freecarpoolkarpoarr: []


    signal detailsSave()
    signal detailsCancel()


    function init()
    {

        showownernamecheckbox.setSelected((showownername == "1"))
        showownerphonecheckbox.setSelected((showownerphone == "1"))
        showownernhometowncheckbox.setSelected((showownerhometown == "1"))

        var carpoollist = new DCDATA.DCCarpoolList();
        carpoollist.karpoid = carpool.karpoid
        carpoollist.password = carpool.password

        DCDATA.send(carpoollist, datacloudurl, "getcarpoolsbyowner", getfreecarpoolscb)

        visible = true
        messagebox.showInfo("Finding free carpools...")
    }

    function getfreecarpoolscb(response)
    {
        console.log("getfreecarpoolscb: " + response.statusString)

        var carpoolarray = []
        var carpoolnamearray = []
        routedetailsdlg.carpoolarrind = -1
        var ind = 0
        for (var i = 0; i < response.carpoollist.length; i++)
        {
            if (response.carpoollist[i].routekarpoid == "0" || response.carpoollist[i].routekarpoid == routekarpoid)
            {
                carpoolarray[ind] = response.carpoollist[i]
                carpoolnamearray[ind] = response.carpoollist[i].name
                ind++
            }

        }
        routedetailsdlg.freecarpoolkarpoarr = carpoolarray

        carpoolcombobox.setComboItems(carpoolnamearray);
        if (carpoolarray.length > 0)
        {
            // set the route's carpool as selected if there are more than one carpools available
            var cpind = 0
            if (routekarpoid != "0")
            {
                for (var c = 0;c < carpoolarray.length; c++)
                {
                    if (routekarpoid == carpoolarray[c].routekarpoid)
                    {
                        cpind = c
                    }
                }
            }
            carpoolcombobox.setSelectedItem(carpoolnamearray[cpind])
            routedetailsdlg.carpoolarrind = cpind
        }
        messagebox.visible = false

    }

    function clearData()
    {
        routedetailsdlg.routename = ""
        routedetailsdlg.routedescriptions = ""
        routedetailsdlg.routetime = ""
        routedetailsdlg.setRepeat("No repeat")
        routedetailsdlg.showownername=PS.showmyname
        routedetailsdlg.showownerhometown=PS.showmyhometown
        routedetailsdlg.showownerphone=PS.showmyphone
        routedetailsdlg.routekarpoid = "0"
        routedetailsdlg.routecarpoolkarpoid = "0"
        routedetailsdlg.carpoolarrind = -1


    }

    function setRepeat(repeat)
    {
        routerepeat = repeat
        repeatcombobox.setSelectedItem(repeat)
    }



    Rectangle
    {
        id: titlerect
        width: 110
        height: 29
        color: "#5d8eac"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        Text {
            id: titleid
            width: 110
            height: 29
            text: "Route details"
            font.family: "Arial"
            anchors.leftMargin: 10
            anchors.fill: parent
            anchors.verticalCenter: titlerect.verticalCenter
            font.pixelSize: 16
        }

        MouseArea {
            id: titlerectmousearea
            anchors.fill: parent

            drag.target: routedetailsdlg
            drag.axis: Drag.XandYAxis
            drag.minimumX: 0
            drag.maximumX: 1000
            drag.minimumY: 0
            drag.maximumY: 1000
        }
    }

    property int labelwidth: 75
    property int labelfontpixesize: 14
    property int inputboxheight: 26

    Rectangle
    {
        id: routenamerect
        color: "#00000000"
        height: routedetailsdlg.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: titlerect.bottom
        anchors.topMargin: 5

        Text {
            id: routenameid
            width: routedetailsdlg.labelwidth
            text: "Name"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: routenamerect.verticalCenter
            font.pixelSize: routedetailsdlg.labelfontpixesize
        }

        Rectangle {
            id: routenameinputrectangle
            height: routedetailsdlg.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: routenameid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: routenameinput
                text: ""
                anchors.leftMargin: 3
                anchors.topMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: routedetailsdlg.labelfontpixesize
            }
        }
    }

    Rectangle
    {
        id: routedescriptionrect
        color: "#00000000"
        height: routedetailsdlg.labelfontpixesize*3
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: routenamerect.bottom
        anchors.topMargin: 5

        Text {
            id: routedescriptionid
            width: routedetailsdlg.labelwidth
            text: "Description"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.top: parent.top
            anchors.topMargin: 7
            font.pixelSize: routedetailsdlg.labelfontpixesize
        }

        Rectangle {
            id: routedescriptioninputrectangle
            height: routedetailsdlg.labelfontpixesize*5
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: routedescriptionid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextEdit  {
                id: routedescriptioninput
                text: ""
                anchors.leftMargin: 3
                anchors.topMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                wrapMode: TextEdit.Wrap
                font.pixelSize: routedetailsdlg.labelfontpixesize
            }
        }
    }

    Rectangle
    {
        id: routetimerect
        color: "#00000000"
        height: routedetailsdlg.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: routedescriptionrect.bottom
        anchors.topMargin: 5

        Text {
            id: routetimeid
            width: routedetailsdlg.labelwidth
            text: "Time"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: routetimerect.verticalCenter
            font.pixelSize: routedetailsdlg.labelfontpixesize
        }

        Rectangle {
            id: routetimeinputrectangle
            height: routedetailsdlg.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: routetimeid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: routetimeinput
                text: ""
                anchors.leftMargin: 3
                anchors.topMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: routedetailsdlg.labelfontpixesize
            }
        }
    }


    Rectangle
    {

        id: repeatrect
        color: "#00000000"
        height: routedetailsdlg.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: routetimerect.bottom
        anchors.topMargin: 5
        z: 55

        Text {
            id: repeatid
            width: routedetailsdlg.labelwidth
            text: "Repeat"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: repeatrect.verticalCenter
            font.pixelSize: routedetailsdlg.labelfontpixesize
        }

        ComboBox {
            id: repeatcombobox
            deletebutton: false
            height: routedetailsdlg.inputboxheight
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: repeatid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            onItemSelected:
            {
                routerepeat = label
            }

        }

        Component.onCompleted:
        {
            repeatcombobox.setComboItems(["No repeat","Daily","Weekly","Monthly"])
            repeatcombobox.setSelectedItem(routerepeat)
        }
    }

    Rectangle
    {

        id: carpoolrect
        color: "#00000000"
        height: routedetailsdlg.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: repeatrect.bottom
        anchors.topMargin: 5
        z: 54

        Text {
            id: carpoolid
            width: routedetailsdlg.labelwidth
            text: "Carpool"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: carpoolrect.verticalCenter
            font.pixelSize: routedetailsdlg.labelfontpixesize
        }

        ComboBox {
            id: carpoolcombobox
            deletebutton: false
            height: routedetailsdlg.inputboxheight
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: carpoolid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom


            onItemSelected:
            {
                routedetailsdlg.carpoolarrind = carpoolcombobox.selectedind
            }

        }

    }

    Rectangle
    {

        id: privacyrect
        color: "#00000000"
        height: routedetailsdlg.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: carpoolrect.bottom
        anchors.topMargin: 5
        z: 4

        Text {
            id: privacyid
            width: routedetailsdlg.labelwidth
            text: "Show my"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: privacyrect.verticalCenter
            font.pixelSize: routedetailsdlg.labelfontpixesize
        }


        CheckBox
        {
            id: showownernamecheckbox
            width: 80
            label: "name"
            textfontpixelsize: 12
            selected: (routedetailsdlg.showownername=="1")
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.left: privacyid.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top

            onSelectionchanged:
            {
                isSelected?routedetailsdlg.showownername="1":routedetailsdlg.showownername="0"
            }
        }


        CheckBox
        {
            id: showownerphonecheckbox
            width: 70
            label: "phone"
            textfontpixelsize: 12
            selected: (routedetailsdlg.showownerphone=="1")
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.left: showownernamecheckbox.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top

            onSelectionchanged:
            {
                isSelected?routedetailsdlg.showownerphone="1":routedetailsdlg.showownerphone="0"
            }
        }


        CheckBox
        {
            id: showownernhometowncheckbox
            width: 100
            label: "hometown"
            textfontpixelsize: 12
            selected: (routedetailsdlg.showownerhometown=="1")
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.left: showownerphonecheckbox.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top

            onSelectionchanged:
            {
                isSelected?routedetailsdlg.showownerhometown="1":routedetailsdlg.showownerhometown="0"
            }
        }
    }
    PushButton
    {
        id: saveid
        label: "Save"
        width: 80
        height: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10

        onButtonpressed:
        {
            if (carpoolarrind < 0)
            {
                messagebox.showInfo("Select the carpool for this route.")
            }
            else
            {
                routedetailsdlg.detailsSave()
            }
        }

    }

    PushButton
    {
        id: cancelid
        label: "Cancel"
        width: 80
        height: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.right: saveid.left
        anchors.rightMargin: 20

        onButtonpressed:
        {
           routedetailsdlg.detailsCancel()
        }

    }

    MessageBox
    {
        id: messagebox

        function showInfo(msg)
        {
            messagebox.text = msg
            messagebox.show("")
        }
    }

}
